<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { queryShortTermDetail, adjustShortTermBikeFee } from '@/api/order'
import { queryShortTermDetailResponseData } from '@/api/order/types/order'
import CustomFormItem from '@/components/basic/CustomFormItem.vue'
import { getToken } from '@/utils/cache/cookies'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { ShortTermAdjustType } from '@/types/enum'
const dialogVisible = ref<boolean>(false)
const orderId = ref<string>('')
const userId = ref<string>('')
const open = () => {
  getAdjustInfo()
  dialogVisible.value = true
}
const setId = (newId: string, newUserId: string) => {
  orderId.value = newId
  userId.value = newUserId
}
defineExpose({ open, setId })
const emits = defineEmits(['refresh'])
const data = ref<queryShortTermDetailResponseData | null>(null)

const getAdjustInfo = async () => {
  try {
    const res = await queryShortTermDetail({
      token: getToken() ?? '',
      orderId: orderId.value
    })
    if (res.statusCode === '0') {
      data.value = res.data
    }
  } catch (error) {
    console.error(error)
  }
}
const form = reactive({
  adjustFee: 0,
  type: 0,
  reason: ''
})
const formRef = ref<FormInstance | null>(null)
const rules = reactive<FormRules>({
  type: [
    {
      required: true,
      message: '请选择调整类型',
      trigger: 'change'
    }
  ],
  reason: [
    {
      required: true,
      message: '请输入调整原因',
      trigger: 'blur'
    }
  ],
  adjustFee: [
    {
      required: true,
      message: '请输入减少待支付金额',
      trigger: 'blur'
    },
    {
      type: 'pattern',
      message: '减少待支付金额不能为负数',
      trigger: 'blur',
      pattern: /^(0|[1-9]\d*)(\.\d+)?$/
    }
  ]
})
const cancle = () => {
  dialogVisible.value = false
}
const confirm = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      adjustShortTermBikeFee({
        token: getToken() ?? '',
        orderId: orderId.value,
        adjustFee: form.adjustFee,
        type: form.type as ShortTermAdjustType,
        reason: form.reason,
        userId: userId.value
      })
        .then((res) => {
          if (res.statusCode === '0') {
            ElMessage.success(res.data)
            emits('refresh')
            cancle()
          }
        })
        .catch((error) => {
          console.error(error)
        })
    }
  })
}
const handleClose = () => {
  formRef.value?.resetFields()
}
/** 控制强制还车费的禁选 */
const disPatchFeeDisabled = computed(() => !Number(data.value?.dispatchFee))
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    title="待付调整"
    width="750px"
    class="adjust-fee-dialog"
    @close="handleClose"
  >
    <el-form :model="form" ref="formRef" :rules="rules" label-width="130px">
      <custom-form-item :label-width="130">
        <template #label> 用户手机号:</template>
        <template #value>
          <span>{{ data?.phone }}</span>
        </template>
        <template #label2>行程编号:</template>
        <template #value2>
          <span>{{ data?.orderId }}</span></template
        >
      </custom-form-item>
      <custom-form-item :label-width="130">
        <template #label> 骑行时长:</template>
        <template #value>
          <span>{{ data?.rideDuration }}</span>
        </template>
        <template #label2>骑行金额:</template>
        <template #value2>
          <span>{{ data?.rent }}元</span></template
        >
      </custom-form-item>
      <custom-form-item :label-width="130">
        <template #label> 强制还车金额:</template>
        <template #value>
          <span class="text-[#FF6D75]">{{ data?.dispatchFee }}元</span>
        </template>
        <template #label2>已支付金额:</template>
        <template #value2>
          <span class="text-[#00A870]"> {{ data?.deductionAmount }}元</span>
        </template>
      </custom-form-item>
      <custom-form-item :label-width="130">
        <template #label> 抵扣方式:</template>
        <template #value>
          <span>{{ data?.deductionType }}</span>
        </template>
        <template #label2>待支付金额:</template>
        <template #value2>
          <span class="text-warning-light inline-flex flex-wrap"
            ><span>{{ data?.waitPayAmount }}元</span>
            <span class="text-[#999999] text-[12px] ml-5">{{ data?.waitPayRemark }}</span></span
          >
        </template>
      </custom-form-item>
      <el-form-item prop="type" label="调整类型">
        <el-radio-group v-model="form.type">
          <el-radio :value="0" label="骑行费" />
          <el-radio :value="1" label="强制还车费" :disabled="disPatchFeeDisabled" />
          <el-radio :value="2" label="维修费" />
          <el-radio :value="3" label="拖车费" />
          <el-radio :value="4" label="违规费" />
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="adjustFee" label="减少待支付金额">
        <el-input v-model="form.adjustFee" type="number">
          <template #prepend>￥</template>
        </el-input>
        <span class="text-[12px] text-[#999999] leading-17 mt-5"
          >只支持对未支付的行程费、违规费进行调整，调整后用户支付剩余金额即可</span
        >
      </el-form-item>
      <el-form-item prop="reason" label="原因">
        <el-input v-model="form.reason" :min="2" placeholder="请输入原因" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button plain @click="cancle">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped></style>
